{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 5.对齐运算\n",
    "# 行列索引名必须都一样"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1\n",
      "1    2\n",
      "2    3\n",
      "dtype: int64\n",
      "a    4\n",
      "b    5\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "s1+s2: \n",
      "0   NaN\n",
      "1   NaN\n",
      "2   NaN\n",
      "a   NaN\n",
      "b   NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "s1 = pd.Series(range(1,4), index = range(3))\n",
    "s2 = pd.Series(range(4,6), index = range(2))\n",
    "print(s1)\n",
    "print(s2)\n",
    "print('-'*50)\n",
    "\n",
    "# Series 对齐运算\n",
    "print('s1+s2: ')\n",
    "s3=s1+s2\n",
    "print(s3)  #缺失数据默认是NaN  np.nan"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T05:26:51.849834500Z",
     "start_time": "2024-05-01T05:26:51.813674100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1,)\n",
      "<class 'numpy.ndarray'>\n",
      "[2 3 4 5 6]\n",
      "--------------------------------------------------\n",
      "()\n",
      "<class 'numpy.ndarray'>\n",
      "[2 3 4 5 6]\n"
     ]
    }
   ],
   "source": [
    "#两个长度不同的一维ndarray相加\n",
    "a1 = np.array([1,2,3,4,5])\n",
    "a2 = np.array([1]) # 长度为1\n",
    "\n",
    "print(a2.shape)\n",
    "print(type(a2))\n",
    "print(a1+a2)\n",
    "print('-'*50)\n",
    "\n",
    "a3 = np.array(1)\n",
    "print(a3.shape)\n",
    "print(type(a3))\n",
    "print(a1+a3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-01T05:16:12.451626200Z",
     "start_time": "2024-05-01T05:16:12.435671100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Series的对齐运算add、sub"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "--------------------------------------------------\n",
      "0    5.0\n",
      "1    7.0\n",
      "2    3.0\n",
      "dtype: float64\n",
      "--------------------------------------------------\n",
      "0    3.0\n",
      "1    3.0\n",
      "2    3.0\n",
      "dtype: float64\n",
      "0   -3.0\n",
      "1   -3.0\n",
      "2   -3.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(np.isnan(s3[2]))\n",
    "print('-'*50)\n",
    "\n",
    "print(s2.add(s1, fill_value = 0))  #缺失的数据将用填充值做运算\n",
    "print('-'*50)\n",
    "\n",
    "print(s2.sub(s1, fill_value = 6))\n",
    "print(s1.sub(s2, fill_value = 6))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-01T05:19:34.963677400Z",
     "start_time": "2024-05-01T05:19:34.930572300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# DataFrame的对齐运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     a    b\n",
      "a  1.0  1.0\n",
      "b  1.0  1.0\n",
      "     a    b    c\n",
      "0  1.0  1.0  1.0\n",
      "1  1.0  1.0  1.0\n",
      "2  1.0  1.0  1.0\n",
      "--------------------------------------------------\n",
      "    a   b   c\n",
      "a NaN NaN NaN\n",
      "b NaN NaN NaN\n",
      "0 NaN NaN NaN\n",
      "1 NaN NaN NaN\n",
      "2 NaN NaN NaN\n",
      "     a    b    c\n",
      "0 -1.0 -1.0 -1.0\n",
      "1 -1.0 -1.0 -1.0\n",
      "2 -1.0 -1.0 -1.0\n",
      "a  1.0  1.0  NaN\n",
      "b  1.0  1.0  NaN\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "df1 = pd.DataFrame(np.ones(shape = (2,2)), columns = ['a', 'b'])\n",
    "df2 = pd.DataFrame(np.ones((3,3)), columns = ['a', 'b', 'c'])\n",
    "print(df1)\n",
    "print(df2)\n",
    "print('-'*50)\n",
    "\n",
    "print(df1-df2)\n",
    "print(df2.sub(df1, fill_value = 2)) #未对齐的数据将和填充值做运算"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T05:25:07.137643200Z",
     "start_time": "2024-05-01T05:25:07.084952700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
